using System;
using System.Runtime.InteropServices;
using System.Security.Permissions;
using System.Collections.Specialized;
using System.Collections.Generic;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Security;
using Microsoft.SharePoint.Administration;
using CreateBcsEntity;

namespace DuetBcsActivities.Features.DuetBcsActivities.Activities
{
    /// <summary>
    /// This class handles events raised during feature activation, deactivation, installation, uninstallation, and upgrade.
    /// </summary>
    /// <remarks>
    /// The GUID attached to this class may be used during packaging and should not be modified.
    /// </remarks>

    [Guid("30bb43a6-1f84-4874-867a-05f867ce6269")]
    public class DuetBcsActivitiesEventReceiver : SPFeatureReceiver
    {

        private const string FeatureId = "1e0ef476-5ff7-40c3-bc61-99878d272323";
        // Uncomment the method below to handle the event raised after a feature has been activated.

        public override void FeatureActivated(SPFeatureReceiverProperties properties)
        {
            SPWebApplication webApp = properties.Feature.Parent as SPWebApplication;

            foreach (SPWebConfigModification mod in CreateConfigModifications())
            {
                webApp.WebConfigModifications.Add(mod);
            }

            webApp.Farm.Services.GetValue<SPWebService>().ApplyWebConfigModifications();
            webApp.Update();
        }



        public override void FeatureDeactivating(SPFeatureReceiverProperties properties)
        {
            SPWebApplication webApp = properties.Feature.Parent as SPWebApplication;

            foreach (SPWebConfigModification mod in CreateConfigModifications())
            {
                webApp.WebConfigModifications.Remove(mod);
            }

            webApp.Farm.Services.GetValue<SPWebService>().ApplyWebConfigModifications();
            webApp.Update();
        }

        private List<SPWebConfigModification> CreateConfigModifications()
        {
            List<SPWebConfigModification> configModifications = new List<SPWebConfigModification>();

            SPWebConfigModification actionRecord = new SPWebConfigModification();
            actionRecord.Path = "configuration/System.Workflow.ComponentModel.WorkflowCompiler/authorizedTypes";
            string WorkflowActionAssembly = typeof(CreateBcsEntity.CreateBcsEntityActivity).Assembly.FullName;
            actionRecord.Name = String.Format("authorizedType[Assembly='{0}']", WorkflowActionAssembly);
            actionRecord.Sequence = 0;
            actionRecord.Owner = FeatureId;
            actionRecord.Type = SPWebConfigModification.SPWebConfigModificationType.EnsureChildNode;
            actionRecord.Value = string.Format("<authorizedType Assembly=\"{0}\" Namespace=\"CreateBcsEntity\"  TypeName=\"*\" Authorized=\"True\" />",
               WorkflowActionAssembly);

            configModifications.Add(actionRecord);

            return configModifications;
        }

    }
}
